﻿Imports MeoTom.Core

Namespace Control

    Partial Public Class LinkList
        Inherits UserControl
        Public Lc As New BLL.LinkCat()
        Public L As New BLL.Link()
        Public Util2 As New Utilities.Common()

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim previousPage As String = ""
            If Request.QueryString.ToString = vbNullString Then
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
            Else
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
            End If
            Util2.ValidateLogin(previousPage)

            BindGrid()
            mycat.InnerHtml = Lc.WriteLinkCat(4)
        End Sub

        Public Sub BindGrid()
            Select Case Request.QueryString("action")
                Case "getByCat"
                    l.LinkCat = Integer.Parse(Request.QueryString("CatID"))
                    grvLinkMgr.DataSource = l.GetAll()
                    grvLinkMgr.DataBind()
                Case "Delete"
                    DeleteLink(Request.QueryString("ID"))
                    lc.WriteLinkCat(5)
                    Response.Redirect("/mt-inside/LinkManager.aspx", False)
                Case "Edit"
                Case Else
                    grvLinkMgr.DataSource = l.GetAll()
                    grvLinkMgr.DataBind()
            End Select
        End Sub

        Private Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
            If Request.Form("selectedAction0") = "2" Or Request.Form("selectedAction1") = "2" Then
                'Delete
                For Each str As String In Regex.Split(Request.Form("cboCheckLink"), ",")
                    If util2.IsNumeric(str) Then
                        DeleteLink(str)
                    End If
                Next
                lc.WriteLinkCat(5)
                BindGrid()
            End If
        End Sub

        Private Sub DeleteLink(ByVal strID As String)
            l.LinkID = Integer.Parse(strID)
            l.Delete()
        End Sub

        Private Sub btnApply2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply2.Click
            btnApply_Click(sender, e)
        End Sub

        Private Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
            Response.Redirect("/mt-inside/LinkManager.aspx?action=getByCat&CatID=" & Request.Form("cboCategory"))
        End Sub

        Public Function DisplayCategory(ByVal LinkID As String) As String
            l.LinkID = Integer.Parse(LinkID)
            Return l.GetAllCat(0)
        End Function

        Public Function DisplayStatus(ByVal IsPrivate As String) As String
            If IsPrivate = "True" Then
                Return "Yes"
            Else
                Return "No"
            End If
        End Function

        Private Sub grvLinkMgr_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grvLinkMgr.PageIndexChanging
            grvLinkMgr.PageIndex = e.NewPageIndex
            BindGrid()
        End Sub
    End Class
End Namespace